import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:video_player/video_player.dart';

class VideoWidget extends StatefulWidget {
  final double width;
  final double height;
  bool isPlay;

  VideoWidget({Key key, this.width, this.height, this.isPlay = true})
      : super(key: key);

  _VideoWidgetState createState() => _VideoWidgetState();
}

class _VideoWidgetState extends State<VideoWidget> {
  VideoPlayerController videoPlayerController;
  ChewieController chewieController;

  void initState() {
    super.initState();
    //配置视频地址cv
    videoPlayerController = VideoPlayerController.network(
      'https://video.wezhan.cn/79f5b46e228d48999df6cdf2526db09d/44cdfe7ee8c64be2aff56c93de10483c-60eee9b151104fa8e02ad8fd7ee8e2c1-sd.mp4?auth_key=1600332543-d213d1c583d445628c33c5cb1ec4d796-0-e63fea033aab7d47525e4f036ba2716d',
    );
    chewieController = ChewieController(
      videoPlayerController: videoPlayerController,
      aspectRatio: 16 / 9, //宽高比
      autoPlay: widget.isPlay, //自动播放
      looping: false, //循环播放
    );
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        setState(() {
          widget.isPlay = false;
        });
      },
      child: Container(
        constraints: BoxConstraints(
            minWidth: 1000,
            maxHeight: 900
        ),
        padding: EdgeInsets.only(top: 22.4,bottom: 22.6),
        alignment: Alignment.center,
        child: Stack(
          children: [
            Container(
              color: Colors.black,
              child: Chewie(
                controller: chewieController,
              ),
            ),
            widget.isPlay
                ? Stack(
                    alignment: Alignment.center,
                    children: [
                      Image.network(
                        'http://nwzimg.wezhan.hk/contents/sitefiles3604/18024687/images/2332719.jpg',
                        fit: BoxFit.fill,
                      ),
                      Container(
                        width: 1600,
                        height: 900,
                        alignment:Alignment.center,
                        child: Icon(
                          Icons.slow_motion_video_sharp,
                          color: Colors.white,
                          size: 50,
                        ),
                      )
                    ],
                  )
                : SizedBox(),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    videoPlayerController.dispose();
    chewieController.dispose();
    super.dispose();
  }
}
